//
//  HMNavigationController.swift
//  Weibo
//
//  Created by 曾成 on 2017/6/4.
//  Copyright © 2017年 WickzZ. All rights reserved.
//

import UIKit

class HMNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //设置屏幕边缘设置代理
        self.interactivePopGestureRecognizer?.delegate = self
        
    }

    
    override func pushViewController(_ viewController: UIViewController, animated: Bool){
    
    //override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0{
        
            if viewControllers.count == 1{
            
                let title = viewControllers.first!.title
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title!, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            }else{
            
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
                
            }
            
            viewController.title = "当前显示的\(viewControllers.count + 1)层控制器"
            
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    @objc private func backAction(){
    
        self.popViewController(animated: true)
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count == 1{
        
            return false
        }
        return true
    }
   

}
